package com.fofia.iotweb.web.controllers.iot;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.fofia.iotweb.framework.core.BaseController;
import com.fofia.iotweb.framework.core.RequestHelper;
import com.fofia.iotweb.web.domain.service.SmartTagService;
import com.fofia.iotweb.web.dto.DataTableDto;
import com.fofia.iotweb.web.dto.JsonResult;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.ModelAndView;

@Mapping("/iot/tag")
@Controller
public class TagController extends BaseController {


    @Inject
    private RequestHelper requestHelper;

    @Inject
    private SmartTagService smartTagService;


    @Get
    @Mapping("index")
    @AuthPermissions("#")
    public ModelAndView index(){
        String today= DateUtil.today();
        return view().put("stime",today).put("etime",today);
    }

    /**
     * 分页数据，按时间降序
     * @return
     */
    @Post
    @Mapping("getHistoryPageList")
    @AuthPermissions("#")
    public DataTableDto getHistoryPageList() {

        String tagId=requestHelper.getString("tagId","");
        String gatewayId=requestHelper.getString("gatewayId","");
        int page=requestHelper.getInt("pageNumber",1);
        int pageSize=requestHelper.getInt("pageSize",10);
        String stime=requestHelper.getString("queryStartDay","");
        String etime=requestHelper.getString("queryEndDay","");
        String lastPageUploadTime=requestHelper.getString("lastPageUploadTime","");
        int inTime=requestHelper.getInt("inTime",0);//数据记录是否包含历史记录，为1则全部是实时数据
        //StaticLog.info("getPageList:{} {} {} {},{}",tagId,page,pageSize,stime,etime);

        if(StrUtil.isEmpty(tagId)){
            return JsonResult.table("请输入标签id");
        }
        if(StrUtil.isEmpty(stime)){
            return JsonResult.table("请输入开始时间");
        }
        if(StrUtil.isEmpty(etime)){
            return JsonResult.table("请输入结束时间");
        }

        stime=StrUtil.format("{} 00:00:00",stime);
        etime=StrUtil.format("{} 23:59:59",etime);
        try{
            var list=smartTagService.getPageList(tagId,gatewayId,page,pageSize,stime,etime,inTime);
            return list;

        }catch (Exception ex){
            StaticLog.error(ex);
        }

        return JsonResult.table();
    }
}
